package com.wxw.springcloud.hystrix;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

/**
 * @author wxw
 * 自定义Hystrix
 * @data 2021/1/24 19 :26
 * @description
 */

public class MyHystrixCommand extends HystrixCommand {
       private RestTemplate restTemplate;

    public MyHystrixCommand(Setter setter, RestTemplate restTemplate) {

        super(setter);
        this.restTemplate=restTemplate;
    }

    @Override
    protected String run() throws Exception {
        //调用远程服务
        return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/hystrix", String.class).getBody();
    }

    /**
     * 当远程服务不可用时会触发
     * @return
     */
    @Override
    public String getFallback(){
        Throwable exception = super.getExecutionException();
        System.out.println(exception.getMessage());
        return  "error";
    }
}
